package com.yootk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yootk.dao.IMemberDao;
import com.yootk.service.IMemberService;
import com.yootk.vo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private IMemberDao memberDao;

    // 用户列表
    @Override
    public List<Member> list() {
        return memberDao.findAll();
    }

    // 根据id查询
    @Override
    public Member get(String mid) {
        return memberDao.selectById(mid);
    }

    // 增加数据
    @Override
    public boolean add(Member member) {
        return memberDao.insert(member) > 0;
    }

    // 数据删除
    @Override
    public boolean delete(Set<String> ids) {
        return memberDao.deleteBatchIds(ids) == ids.size();
    }

    // 分页查询
    @Override
    public IPage<Member> listSplit(String column, String keyword, Integer currentPage, Integer lineSize) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(column, keyword);     // 模糊查询
        int count = memberDao.selectCount(queryWrapper);
        IPage<Member> page = new Page<>(currentPage, lineSize, count);
        return memberDao.selectPage(page, queryWrapper);
    }

    // 批量数据增加
    @Override
    public boolean addBatch(String... mid) {
        for (String id : mid) {
            Member member = new Member();
            member.setMid(id);  // 如果id重复则会出现更新异常
            member.setName("爆可爱的小李老师");
            memberDao.insert(member);
        }
        return true;
    }
}
